/**
 * Created by Yet.
 */

import { Cookies } from 'js-cookie'
import { validatenull } from '@/utils/validate'

export interface CookieParams {
  key: string
  value: any
  expireDate: Date
}

/**
 * 存储localStorage
 */

export const setStore = (params: { name: any; content?: any; type?: string }) => {
  const { name, content, type } = params
  const obj = {
    dataType: typeof content,
    content,
    type,
    datetime: new Date().getTime()
  }
  if (type) window.sessionStorage.setItem(name, JSON.stringify(obj))
  else window.localStorage.setItem(name, JSON.stringify(obj))
}
/**
 * 获取localStorage
 */
export const getStore = (params: { name: any; type?: string; content?: any }) => {
  const { name } = params
  let obj: any = {}
  let content
  obj = window.localStorage.getItem(name)
  if (validatenull(obj)) obj = window.sessionStorage.getItem(name)
  if (validatenull(obj)) return
  obj = JSON.parse(obj)
  if (obj.dataType === 'string') {
    content = obj.content
  } else if (obj.dataType === 'number') {
    content = Number(obj.content)
  } else if (obj.dataType === 'boolean') {
    /* eslint-disable */
        content = eval(obj.content)
    } else if (obj.dataType === 'object') {
        content = obj.content
    }
    return content
}
/**
 * 删除localStorage
 */
export const removeStore = (params:{name:string}) => {
    const {
        name
    } = params
    window.localStorage.removeItem(name)
    window.sessionStorage.removeItem(name)
}

/**
 * 获取全部localStorage
 */
export const getAllStore = (params:{type:string}) => {
    const list = []
    const {
        type
    } = params
    for (let i = 1; i <= window.sessionStorage.length; i++) {
        if (type) {
            list.push({
                name: window.sessionStorage.key(i),
                content: getStore({
                    name: window.sessionStorage.key(i),
                    type: 'session'
                })
            })
        } else {
            list.push(getStore({
                name: window.localStorage.key(i),
                content: getStore({
                    name: window.localStorage.key(i)
                })
            }))
        }
    }

    return list
}

/**
 * 清空全部localStorage
 */
export const clearStore = (params:{type:string}) => {
    const {
        type
    } = params
    if (type) {
        window.sessionStorage.clear()
        return
    }
    window.localStorage.clear()
}

/**
 * 存储cookie
 */
export const setCookie = (params:{key:string,value:any,expireDate:Date}) => {
    const {
        key,
        value,
        expireDate
    } = params
    debugger
    if(expireDate){
        return Cookies.set(key, value, expireDate)
    }else{
        return Cookies.set(key, value)
    }
}

/**
 * 读取cookie
 */
export const getCookie = (params:{key:string}) =>{
    const {
        key
    } = params
    return Cookies.get(key)
}

/**
 * 删除cookie
 */
export const removeCookie = (params:{key:string}) =>{
    const{
        key
    } = params
    return Cookies.remove(key)
}
